package com.xiaochong.tir.api.webws;

import com.xc.logclient.utils.LogTrace;
import com.xiaochong.tir.api.config.ExecutorConfig;
import com.xiaochong.tir.api.service.WebSocketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.websocket.Session;

/**
 * @Project : xc-tir
 * @Package Name : com.xiaochong.tir.api.webws
 * @Description : TODO
 * @Author : daizhiqing@xiaochong.com
 * @Creation Date : 2018年06月13日下午3:11
 * @ModificationHistory Who When What
 */
@Component("asyncPush")
public class AsyncPush {

    @Autowired
    WebSocketService webSocketService;

    public  static final long  WAIT_TIME = 10000;

    @Async(ExecutorConfig.WS_REQ_ASYNC)
    public void orderBook(Session session, int exchangeId, int transactionId) {

        while (session.isOpen()) {
            LogTrace.info("orderBook","发送orderBook数据" ,session.getId());
            String orderBookMsg = webSocketService.orderBook(exchangeId, transactionId);
            if (orderBookMsg != null) {
                try {
                    session.getBasicRemote().sendText(orderBookMsg);
                }catch (Exception e){
                    continue;
                }
            }else {
                LogTrace.info("orderBook","orderBook数据NULL" ,session.getId());
            }
            try {
                Thread.sleep(WAIT_TIME);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    @Async(ExecutorConfig.WS_REQ_ASYNC)
    public void kAction(Session session, int exchangeId, int transactionId) {
        while (session.isOpen()) {
            LogTrace.info("kAction","发送kAction数据" ,session.getId());
            String orderBookMsg = webSocketService.kAction(exchangeId, transactionId);
            if (orderBookMsg != null) {
                try {
                    session.getBasicRemote().sendText(orderBookMsg);
                }catch (Exception e){
                    continue;
                }
            }else {
                LogTrace.info("kAction","kAction数据NULL" ,session.getId());
            }
            try {
                Thread.sleep(WAIT_TIME+100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    @Async(ExecutorConfig.WS_REQ_ASYNC)
    public void tranes(Session session, int exchangeId, int transactionId) {
        while (session.isOpen()) {
            LogTrace.info("tranes","发送tranes数据" ,session.getId());

            String orderBookMsg = webSocketService.tranes(exchangeId, transactionId);
            if (orderBookMsg != null) {
                try {
                    session.getBasicRemote().sendText(orderBookMsg);
                }catch (Exception e){
                    continue;
                }
            }else {
                LogTrace.info("tranes","tranes数据NULL" ,session.getId());
            }
            try {
                Thread.sleep(WAIT_TIME+200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }


    @Async(ExecutorConfig.WS_REQ_ASYNC)
    public void depth(Session session, int exchangeId, int transactionId) {
        while (session.isOpen()) {
            LogTrace.info("depth","发送depth数据" ,session.getId());

            String orderBookMsg = webSocketService.depth(exchangeId, transactionId);
            if (orderBookMsg != null) {
                try {
                    session.getBasicRemote().sendText(orderBookMsg);
                }catch (Exception e){
                    continue;
                }
            }else {
                LogTrace.info("depth","depth数据NULL" ,session.getId());

            }
            try {
                Thread.sleep(WAIT_TIME+300);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    @Async(ExecutorConfig.WS_REQ_ASYNC)
    public void rise(Session session, int exchangeId, int transactionId) {
        while (session.isOpen()) {
            LogTrace.info("depth","发送rise数据" ,session.getId());

            String orderBookMsg = webSocketService.rise(exchangeId, transactionId);
            if (orderBookMsg != null) {
                try {
                    session.getBasicRemote().sendText(orderBookMsg);
                }catch (Exception e){
                    continue;
                }
            }else {
                LogTrace.info("depth","rise数据NULL" ,session.getId());
            }
            try {
                Thread.sleep(WAIT_TIME+300);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }


}
